<!--
  ~ Copyright (c) 2019. seven studio. @chengdu
  ~ seven studio in chengdu by lb
  ~ this project for snmp tool
  -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>Data-Table 表格</title>
    <link rel="stylesheet" href="/res/frame/layui/css/layui.css">
    <link rel="stylesheet" href="/res/frame/static/css/style.css">
    <link rel="icon" href="/res/frame/static/image/code.png">
</head>
<body class="body">

<!-- 工具集 -->
<div class="my-btn-box">
    <span class="fl">
        <a class="layui-btn btn-add btn-default" id="btn-refresh"><i class="layui-icon">&#x1002;</i></a>
            <a class="layui-btn layui-btn-danger radius btn-delect" data-type="getCheckData" >批量删除</a>
         <a class="layui-btn btn_del btn-default" id="btn-del" onclick="delAll()">删除全部</a>
          <a class="layui-btn btn_del btn-default" id="btn-deal" onclick="dealAll()">全部处理</a>
    </span>
    <span class="fl">
       <span class="fl">
        <span class="layui-form-label">设备名称：</span>
        <div class="layui-input-inline" style="float: left;">
            <input type="text" id='alDevName' autocomplete="off" placeholder="设备名称" class="layui-input">
        </div>
         <span class="layui-form-label">开始时间：</span>
        <div class="layui-input-inline" style="float: left;">
            <input type="text" id='beginDate' placeholder="yyyy-MM-dd" autocomplete="off" placeholder="开始时间"
                   class="layui-input">
        </div>
         <span class="layui-form-label">结束时间：</span>
        <div class="layui-input-inline" style="float: left;">
            <input type="text" id='endDate' autocomplete="off" placeholder="yyyy-MM-dd" placeholder="结束时间"
                   class="layui-input">
        </div>
       <button class="layui-btn" data-type="reload">搜索</button>
    </span>
    </span>
</div>

<!-- 表格 -->

<table class="layui-hide" id="dateTable" lay-filter="devList"></table>
<script type="text/javascript" src="/res/frame/layui/layui.js"></script>
<script type="text/javascript" src="/res/js/index.js"></script>
<script type="text/javascript">
    // layui方法
    layui.use(['table', 'form', 'layer', 'vip_table', 'cookie', 'laydate'], function () {

        // 操作对象
        var form = layui.form
            , table = layui.table
            , layer = layui.layer
            , vipTable = layui.vip_table
            , $ = layui.jquery,
            cookie = layui.cookie,
            laydate = layui.laydate;

        //日期选择框渲染
        laydate.render({
            elem: '#beginDate',
        });
        laydate.render({
            elem: '#endDate',
        });

        // 表格渲染
        var tableIns = table.render({
            elem: '#dateTable'                  //指定原始表格元素选择器（推荐id选择器）
            , height: vipTable.getFullHeight()
            , cols: [[
                {type: 'checkbox'}
                , {field: 'alDevName', title: '设备名', width: 150}
                , {field: 'alDevIp', title: '设备IP', width: 150}
                , {field: 'pkGuid', hide: true}
                , {
                    field: 'alStatus', title: '告警状态', width: 100, templet: function (d) {
                        return d.alStatus != "YCL" ? "未处理" : "已处理";
                    }
                }
                , {
                    field: 'alSendStatus', title: '发送状态', width: 100, templet: function (d) {
                        return d.alSendStatus == "SENT" ? "已发送" : "未发送";
                    }
                }
                , {
                    field: 'alType', title: '设备类型', width: 120, templet: function (d) {
                        return d.alType == "SwitchType" ? "交换机" : "摄像头";
                    }
                }
                , {
                    field: 'alLevel', title: '告警等级', width: 120, templet: function (d) {
                        return d.alLevel == "AL_COMMON" ? "一般告警" : "严重告警";
                    }
                }
                , {field: 'alMsg', title: '告警信息', width: 300}
                , {field: 'alDlName', title: '处理人', width: 80}
                , {field: 'alDlDate', title: '处理时间', width: 160}
                , {field: 'alCreateDate', title: '产生时间', width: 160}
                , {
                    fixed: 'right', title: '操作', width: 180, align: 'center', templet: function (d) {
                        return d.alStatus == "YCL" ? "<a class=\"layui-btn layui-btn-sm layui-btn-radius layui-btn-danger\" lay-event=\"del\">删除</a>" :
                            "<a class=\"layui-btn layui-btn-sm layui-btn-radius layui-btn-normal\" lay-event=\"deal\">处理</a>\n" +
                            "<a class=\"layui-btn layui-btn-sm layui-btn-radius layui-btn-danger\" lay-event=\"del\">删除</a>"
                    }
                }
            ]]
            , id: 'tableReload'
            , url: '/alarm/listByPage'
            , method: 'post'
            , page: true
            , contentType: 'application/json'
            , request: {
                pageName: 'pageNum' //页码的参数名称，默认：page
                , limitName: 'pageSize' //每页数据量的参数名，默认：limit
            }
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.list //解析数据列表
                }
            }, response: {
                statusName: 'code' //规定数据状态的字段名称，默认：code
                , statusCode: "200" //规定成功的状态码，默认：0
                , msgName: 'msg' //规定状态信息的字段名称，默认：msg
                , countName: 'count' //规定数据总数的字段名称，默认：count
                , dataName: 'data' //规定数据列表的字段名称，默认：data
            }
            , loading: false
            , limits: [30, 60, 90, 150, 300]
            , limit: 30 //默认采用30
            , done: function (res, curr, count) {
            }
        });


        //搜索数据
        var active = {
            reload: function () {
                //执行重载
                table.reload('tableReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        beginDate: $("#beginDate").val(),
                        endDate: $("#endDate").val(),
                        alDevName: $("#alDevName").val()
                    }
                });
            },
            getCheckData: function () { //获取选中数据
                var checkStatus = table.checkStatus('tableReload');
                var data = checkStatus.data;
                var ids = [];
                $.each(data, function (i, val) {
                    ids.push(val.pkGuid);
                });
                var params = {
                    "ids": ids
                }
                var s = JSON.stringify(params);

                $.ajax({
                    url: "/manager/delList",
                    type: "post",
                    dataType: "json",
                    data: s,
                    contentType: "application/json",
                    beforeSend: function () {
                        mask = layer.load(1, {shade: [0.8, '&#xe63d']});
                    },
                    success: function (result) {
                        console.log(result)
                        layer.close(mask);
                        if (result.code == 200) {
                            console.log("zzzzzzzzzzz")
                            layer.alert(result.msg);
                            tableIns.reload();
                        } else {
                            layer.alert(result.msg);
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        layer.close(mask);
                        tableIns.reload();
                        console.log(XMLHttpRequest.responseText);
                    }
                });
            }

        };
        $('.my-btn-box .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        // 刷新
        $('#btn-refresh').on('click', function () {
            tableIns.reload();
        });

        table.on('tool(devList)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'deal') {
                layer.confirm('确认处理告警', function (index) {
                    deal(data.pkGuid);
                });
            } else if (layEvent === 'del') {
                layer.confirm('确认删除告警', function (index) {
                    del(data.pkGuid);
                });
            }
        });

        function del(pkGuid) {
            $.ajax({
                url: '/alarm/delAlarm?pkGuid=' + pkGuid,
                type: "get",
                contentType: "application/json",
                success: function (result) {
                    console.log(result)
                    if (result.code === 200) {

                        layer.msg(result.msg);
                        tableIns.reload();
                    } else {
                        layer.msg(result.msg);
                        tableIns.reload();
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest.responseText);

                }
            });
        }

        function deal(pkGuid) {
            var params = {
                "pkGuid": pkGuid,
                "alStatus": 'YCL',
                "alDlName": $.cookie('adminName'),
                "alDlId": $.cookie('adminId')

            }
            $.ajax({
                url: '/alarm/optionAlarm',
                type: "post",
                contentType: "application/json",
                data: JSON.stringify(params),
                success: function (result) {
                    console.log(result)
                    if (result.code === 200) {
                        layer.msg(result.msg);
                        tableIns.reload();
                    } else {
                        layer.msg(result.msg);
                        tableIns.reload();
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest.responseText);

                }
            });
        }

        window.dealAll = function () {
            var params = {
                "pkGuid": $.cookie('adminId'),
                "mName": $.cookie('adminName')
            }
            $.ajax({
                url: '/alarm/dealAll',
                type: "post",
                contentType: "application/json",
                data: JSON.stringify(params),
                success: function (result) {
                    console.log(result)
                    if (result.code === 200) {

                        layer.msg(result.msg);
                        tableIns.reload();
                    } else {
                        layer.msg(result.msg);
                        tableIns.reload();
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest.responseText);

                }
            });
        }

        window.delAll = function () {
            $.ajax({
                url: '/alarm/delall',
                type: "get",
                contentType: "application/json",
                success: function (result) {
                    console.log(result)
                    if (result.code === 200) {
                        layer.msg(result.msg);
                        tableIns.reload();
                    } else {
                        layer.msg(result.msg);
                        tableIns.reload();
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest.responseText);

                }
            });
        }

    });

    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }


    function ref() {
        $('#btn-refresh').on('click', function () {
            tableIns.reload();
        });
    }


</script>
<!-- 表格操作按钮集 -->


</body>
</html>